﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Mtool.FSM;

namespace MToolStudio
{
    public partial class ModifyToken : Form
    {
        public ModifyToken()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private bool isEdit = false;
        private TokenImpl token;
        public TokenImpl Token
        {
            get { return token; }
            set 
            {
                token = value;
                isEdit = true;
                if (token != null)
                {
                    txtbox_Name.Text = token.Name;
                    if (token.InParas != null && token.InParas.Count != 0)
                    {
                        dataGridView1.Rows.Add(token.InParas.Count);
                        for (int i = 0; i < token.InParas.Count; i++)
                        {
                            dataGridView1.Rows[i].Cells[0].Value = token.InParas[i].Name;
                            dataGridView1.Rows[i].Cells[1].Value = token.InParas[i].Description;
                        }
                    }
                    if (token.OutParas != null && token.OutParas.Count != 0)
                    {
                        dataGridView2.Rows.Add(token.OutParas.Count);
                        for (int i = 0; i < token.OutParas.Count; i++)
                        {
                            dataGridView2.Rows[i].Cells[0].Value = token.OutParas[i].Name;
                            dataGridView2.Rows[i].Cells[1].Value = token.OutParas[i].Description;
                        }
                    }
                }
            }
        }

        private TreeNode tokenRoot;
        public TreeNode TokenRoot
        {
            get { return tokenRoot; }
            set { tokenRoot = value;  }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtbox_Name.Text))
            {
                MessageBox.Show("Please enter the token name.");
                return;
            }

            if (isEdit) TokenManager.DeleteDefinition(txtbox_Name.Text);
            
            if (TokenManager.GetDefinition(txtbox_Name.Text) != null)
            {
                MessageBox.Show(string.Format("Token/Function '{0}' has been defined.", txtbox_Name.Text));
            }
            else
            {
                // save the token's information
                if( token == null) token = new TokenImpl();
                token.Name = txtbox_Name.Text;
                token.InParas = GetParametersFromDataGridView(dataGridView1);
                if (token.InParas == null) return;
                token.OutParas = GetParametersFromDataGridView(dataGridView2);
                if (token.OutParas == null) return;
                TokenManager.SetDefinition(token.Name, token);
                if (!isEdit && tokenRoot != null)
                {
                    TreeNode node = new TreeNode();
                    node.Name = token.Name;
                    node.Text = token.Name;
                    node.Tag = token;
                    tokenRoot.Nodes.Add(node);
                    tokenRoot.Expand();
                }
                Close();
            }
        }

        private List<Parameter> GetParametersFromDataGridView(DataGridView view)
        {
            object nobj;
            object dobj;
            List<Parameter> list = new List<Parameter>();
            for (int i = 0; i < view.RowCount; i++) // last row is empty
            {
                Parameter p = new Parameter();
                nobj = view.Rows[i].Cells[0].Value;
                dobj = view.Rows[i].Cells[1].Value;
                
                if (nobj == null || string.IsNullOrEmpty(nobj.ToString()))
                {
                    if (dobj == null || string.IsNullOrEmpty(dobj.ToString())) continue;
                    MessageBox.Show("Parameter name can't be null or empty.");
                    return null;
                }
                p.Name = nobj.ToString();
                p.Description = (dobj == null) ? "" : dobj.ToString();
                list.Add(p);
            }
            return list;
        }
    }
}
